package com.asha.md360player4android.vr.objects;

import android.content.Context;

import com.asha.md360player4android.vr.SharePreferenceManager;
import com.asha.vrlib.CalibrateInfo;
import com.asha.vrlib.objects.MDAbsObject3D;

/**
 * Created by android on 6/26/17.
 */

abstract class FYMDAbsObject3D extends MDAbsObject3D {

    private CalibrateInfo calibrateInfo;

    @Override
    public void onCalibrateChange(CalibrateInfo info, Context context) {
        synchronized (this) {
            calibrateInfo = info;
        }
        reloadObject(info, context);
    }

    abstract void reloadObject(CalibrateInfo info, Context context);

    protected CalibrateInfo getCalibrateInfo(Context context) {
        if (null == calibrateInfo) {
            synchronized (this) {
                if (null == calibrateInfo) {
                    float[] calibrations = SharePreferenceManager.getCalibrate(context);
                    if (null != calibrations && calibrations.length == 5) {
                        float origin_width = calibrations[0];
                        float origin_height = calibrations[1];
                        float radius = calibrations[2];
//                        float center_x = calibrations[3];
//                        float center_y = calibrations[4];
//                        float offset_x = (2 * center_x - origin_width) / origin_width;
//                        float offset_y = (2 * center_y - origin_height) / origin_height;
//                        float n_radius_y = radius / origin_height;
//                        float n_radius_x = radius / origin_width;
                        float offset_x = calibrations[3];
                        float offset_y = calibrations[4];
                        calibrateInfo = new CalibrateInfo((int) origin_width, (int) origin_height);
                        calibrateInfo.setRadius(radius);
//                        calibrateInfo.setCenterXProgress(center_x);
//                        calibrateInfo.setCenterYProgress(center_y);
                        calibrateInfo.setOffsetX(offset_x);
                        calibrateInfo.setOffsetY(offset_y);
                    }
                }
            }
        }
        return calibrateInfo;
    }
}
